package com.oracle.lingshangejia.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.bean.DoctorVideoBean;

import java.util.List;


public class ExpertVideoAdapter extends RecyclerView.Adapter<ExpertVideoAdapter.ViewHolder> {

    private Context context;
    private View.OnClickListener onClickListener;
    private List<DoctorVideoBean.DataBean> data;
    public ExpertVideoAdapter(Context context, List<DoctorVideoBean.DataBean> data){
        this.data=data;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View view =View.inflate(context,R.layout.recyclerview_expert_video,null);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        viewHolder.tv_expert_name.setText(data.get(i).getYsmc());//名字
        viewHolder.tv_expert_y.setText(data.get(i).getYyName()+data.get(i).getKsName());
        viewHolder.tv_expert_sc.setText("擅长领域: "+data.get(i).getScly());
        RequestOptions requestOptions = new RequestOptions().placeholder(R.drawable.ic_info_user)
                .error(R.drawable.ic_info_user).diskCacheStrategy(DiskCacheStrategy.NONE)
                .circleCropTransform();
        Glide.with(context).load(R.drawable.ic_info_user)
                .apply( requestOptions)
                .into(viewHolder.mIv);
        if (!TextUtils.isEmpty(data.get(i).getUrl())){

        Glide.with(context).load(Constant.imgurl+data.get(i).getUrl())
                .apply( requestOptions)
                .into(viewHolder.mIv);
        }
        if (data.get(i).isOnline()){
            viewHolder.mTvOnline.setText("在线");
            viewHolder.mTvOnline.setTextColor(context.getResources().getColor(R.color.colorBlue ));

        }else {
            viewHolder.mTvOnline.setText("离线");
            viewHolder.mTvOnline.setTextColor(context.getResources().getColor(R.color.black ));

        }


        viewHolder.re_start_video.setOnClickListener((v -> {
            if (onClickListener != null){
                onClickListener.onClick(v);
            }
        }));
        viewHolder.re_start_video.setTag(i);
    }

    @Override
    public int getItemCount() {
        return data==null?0:data.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        RelativeLayout re_start_video;
        TextView tv_expert_name;
        TextView tv_expert_y;
        TextView tv_expert_sc;
        ImageView mIv;//头像
        TextView mTvOnline;//在线
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            re_start_video = itemView.findViewById(R.id.re_start_video);
            tv_expert_name=itemView.findViewById(R.id.tv_expert_name);
            tv_expert_y=itemView.findViewById(R.id.tv_expert_y);
            tv_expert_sc=itemView.findViewById(R.id.tv_expert_sc);
            mIv = itemView.findViewById(R.id.ci_video_img);
            mTvOnline = itemView.findViewById(R.id.tv_online);
        }
    }

    public void setOnClickListener(View.OnClickListener onClickListener){

        this.onClickListener = onClickListener;
    }
}
